package com.riddle.admin.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.ueditor.ActionEnter;
import com.google.gson.Gson;
import com.riddle.model.Article;
import com.riddle.model.Category;
import com.riddle.model.User;
import com.riddle.service.ArticleService;
import com.riddle.service.BaseService;
import com.riddle.util.Constant;
import com.riddle.util.JsonUtil;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.QueryModel;
import com.riddle.vmodel.VMsg;

@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController<Article>  {

	@Resource
	private ArticleService articleService;
	
	@RequestMapping()
	public String index() {
		return "admin/article/index";
	}
	
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("admin/article/add");
	}

	@RequestMapping(value="/addarticle", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addArticle(Article article, HttpSession session) {
		article.setCreateTime(new Date());
		article.setAuthor((User)session.getAttribute(Constant.CurrentUser));
		articleService.insert(article);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
	
	@RequestMapping(value="/category", produces="application/json;charset=utf-8")
	@ResponseBody
	public String category() {
		List<Category> res = articleService.getCategories(null);
		return JsonUtil.toJson(res);
	}
	
	@RequestMapping(value="/list", produces="application/json;charset=utf-8")
	@ResponseBody
	public String list(QueryModel queryModel) {
		return super.list(queryModel);
	}
	
	@RequestMapping("/newarticle")
	public String newarticle() {
		return "admin/user/newarticle";
	}
	
	@RequestMapping(value="/addmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addModel(Article article) {
		return super.addModel(article);
	}
	
	@RequestMapping("/modify")
	public ModelAndView modify(Long id) {
		Article article = articleService.query(id);
		return new ModelAndView("admin/article/modify", "article", article);
	}
	
	@RequestMapping(value="/modifymodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String modifyModel(Article article) {
		article.setModifyTime(new Date());
		return super.modifyModel(article);
	}
	
	@RequestMapping(value="/delete", produces="application/json;charset=utf-8")
	@ResponseBody
	public String delete(Long id) {
		return super.delete(id);
	}

	@Override
	protected BaseService getService() {
		// TODO Auto-generated method stub
		return articleService;
	}
}
